/*
 * Middleware
 * Copyright (C) 2010/11
 * Marco Buzzanca, Davide Giuseppe Monaco, Riccardo Pulvirenti, Giuseppe Ravidà,
 * Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: xml_syn_logging.cpp
 */

#include "xml_syn_logging.hpp"
#include "xml_incapsulator.hpp"

using namespace middleware;
using namespace middleware::logging;

//-----------------------------------------------------------------------------
// Constructors and destructors
XmlSynLogger::XmlSynLogger() {
	this->_log_filename = std::string("core.middleware.log.xml");
	this->_peer_identifier = "random_peer";
	this->_log_cta_port = 0; // Invalid
	this->_parser = new xercesc::XercesDOMParser();
	// Initializing parser
	this->InitParser(kLogOpenOverwriteFile, kNormal);
	// Run thread for writing periodically
	this->RunLogFileWriter();
}
XmlSynLogger::XmlSynLogger(std::string filename, std::string peer_id,
		CTAPort cta_port, XmlSynLoggerOpenMode open_mode,
		XmlSynLoggerCompositionMode comp_mode) {
	this->_log_filename = filename + std::string("_") + peer_id +
			std::string(".middleware.log.xml");
	this->_peer_identifier = peer_id;
	this->_log_cta_port = cta_port;
	this->_parser = new xercesc::XercesDOMParser();
	// Initializing parser
	this->InitParser(open_mode, comp_mode);
	// Run thread for writing periodically
	this->RunLogFileWriter();
}
XmlSynLogger::XmlSynLogger(const XmlSynLogger& logger) {
	this->_log_filename = logger._log_filename;
	this->_peer_identifier = logger._peer_identifier;
	this->_log_cta_port = logger._log_cta_port;
	this->_parser = new xercesc::XercesDOMParser();
	// Initializing parser
	this->InitParser(kLogOpenOverwriteFile, kNormal);
	// Run thread for writing periodically
	this->RunLogFileWriter();
}
XmlSynLogger::~XmlSynLogger() {
	if (this->_parser != 0) delete this->_parser;
}
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
Event::Event(void) : _timestamp(XmlSynLogger::LogTimestamp()) { }
Event::Event(std::string issuer) : _timestamp(XmlSynLogger::LogTimestamp()),
		_issuer(issuer) { }
Event::Event(std::string issuer, std::string body)
		: _timestamp(XmlSynLogger::LogTimestamp()), _issuer(issuer),
		_body(body) { }
Event::~Event(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
Error::Error(void) : Event() { }
Error::Error(std::string issuer) : Event(issuer) { }
Error::Error(std::string desc, std::string issuer) : Event(issuer),
		_description(desc) { }
Error::~Error(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
WorkflowSubmitted::WorkflowSubmitted(void) : Event(std::string("UI"))
	{ _workflow = 0; }
WorkflowSubmitted::WorkflowSubmitted(std::string issuer) : Event(issuer)
	{ _workflow = 0; }
WorkflowSubmitted::WorkflowSubmitted(Workflow* workflow, std::string issuer) :
		Event(issuer)
	{ _workflow = workflow; }
WorkflowSubmitted::~WorkflowSubmitted(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
WorkflowEnqueued::WorkflowEnqueued(void) : Event(std::string("MAN"))
	{ _workflow = 0; }
WorkflowEnqueued::WorkflowEnqueued(std::string issuer) : Event(issuer)
	{ _workflow = 0; }
WorkflowEnqueued::WorkflowEnqueued(Workflow* workflow, std::string issuer)
		: Event(issuer)
	{ _workflow = workflow; }
WorkflowEnqueued::~WorkflowEnqueued(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
WorkflowDequeued::WorkflowDequeued(void) : Event(std::string("MAN"))
	{ _workflow = 0; }
WorkflowDequeued::WorkflowDequeued(std::string issuer) : Event(issuer)
	{ _workflow = 0; }
WorkflowDequeued::WorkflowDequeued(Workflow* workflow, std::string issuer)
		: Event(issuer)
	{ _workflow = workflow; }
WorkflowDequeued::~WorkflowDequeued(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
TaskSent::TaskSent(void) : Event(std::string("MAN")) {
	_task_descriptor = 0;
	_addr = 0;
}
TaskSent::TaskSent(std::string issuer) : Event(issuer) {
	_task_descriptor = 0;
	_addr = 0;
}
TaskSent::TaskSent(TaskDescriptor* task_descriptor, Address* addr,
		std::string issuer) : Event(issuer) {
	_task_descriptor = task_descriptor;
	_addr = addr;
}
TaskSent::~TaskSent(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
TaskReceived::TaskReceived(void) : Event(std::string("MAN")) {
	_task_descriptor = 0;
	_addr = 0;
}
TaskReceived::TaskReceived(std::string issuer) : Event(issuer) {
	_task_descriptor = 0;
	_addr = 0;
}
TaskReceived::TaskReceived(TaskDescriptor* task_descriptor, Address* addr,
		std::string issuer) : Event(issuer) {
	_task_descriptor = task_descriptor;
	_addr = addr;
}
TaskReceived::~TaskReceived(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
WorkerDescriptorAdded::WorkerDescriptorAdded(void)
		: Event(std::string("DISC")) {
	_worker_descriptor = 0;
	_addr = 0;
}
WorkerDescriptorAdded::WorkerDescriptorAdded(std::string issuer)
		: Event(issuer) {
	_worker_descriptor = 0;
	_addr = 0;
}
WorkerDescriptorAdded::WorkerDescriptorAdded(
		WorkerDescriptor* worker_descriptor, Address* addr,
		std::string issuer) : Event(issuer) {
	_worker_descriptor = worker_descriptor;
	_addr = addr;
}
WorkerDescriptorAdded::~WorkerDescriptorAdded(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
WorkerDescriptorUpdated::WorkerDescriptorUpdated(void)
		: Event(std::string("DISC")) {
	_worker_descriptor = 0;
	_addr = 0;
}
WorkerDescriptorUpdated::WorkerDescriptorUpdated(std::string issuer)
		: Event(issuer) {
	_worker_descriptor = 0;
	_addr = 0;
}
WorkerDescriptorUpdated::WorkerDescriptorUpdated(
		WorkerDescriptor* worker_descriptor, Address* addr,
		std::string issuer) : Event(issuer) {
	_worker_descriptor = worker_descriptor;
	_addr = addr;
}
WorkerDescriptorUpdated::~WorkerDescriptorUpdated(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
TaskEnqueued::TaskEnqueued(void) : Event(std::string("W")) {
	_task_descriptor = 0;
}
TaskEnqueued::TaskEnqueued(std::string issuer) : Event(issuer) {
	_task_descriptor = 0;
}
TaskEnqueued::TaskEnqueued(TaskDescriptor* task_descriptor,
		std::string issuer) : Event(issuer) {
	_task_descriptor = task_descriptor;
}
TaskEnqueued::~TaskEnqueued(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
TaskDequeued::TaskDequeued(void) : Event(std::string("W")) {
	_task_descriptor = 0;
}
TaskDequeued::TaskDequeued(std::string issuer) : Event(issuer) {
	_task_descriptor = 0;
}
TaskDequeued::TaskDequeued(TaskDescriptor* task_descriptor,
		std::string issuer) : Event(issuer) {
	_task_descriptor = task_descriptor;
}
TaskDequeued::~TaskDequeued(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
StatusUpdateLoop::StatusUpdateLoop(void) : _status_changed(0),
		Event(std::string("W")) {
	_worker_descriptor = 0;
	_old_worker_descriptor = 0;
}
StatusUpdateLoop::StatusUpdateLoop(std::string issuer) : _status_changed(0),
		Event(issuer) {
	_worker_descriptor = 0;
	_old_worker_descriptor = 0;
}
StatusUpdateLoop::StatusUpdateLoop(bool status_changed,
		WorkerDescriptor* worker_descriptor,
		WorkerDescriptor* old_worker_descriptor, std::string issuer)
		: _status_changed(status_changed), Event(issuer) {
	_worker_descriptor = worker_descriptor;
	_old_worker_descriptor = old_worker_descriptor;
}
StatusUpdateLoop::~StatusUpdateLoop(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
TDCCollectorLoopExec::TDCCollectorLoopExec(void) : _collected(0),
		Event(std::string("W")) { }
TDCCollectorLoopExec::TDCCollectorLoopExec(std::string issuer)
		: _collected(0), Event(issuer) { }
TDCCollectorLoopExec::TDCCollectorLoopExec(unsigned int collected,
		std::string issuer) : _collected(collected), Event(issuer) { }
TDCCollectorLoopExec::~TDCCollectorLoopExec(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
ExecTaskStillNotExecuted::ExecTaskStillNotExecuted(void) : _attempt(0),
		_attempts(0), Event(std::string("MAN")) {
	_task_descriptor = 0;
}
ExecTaskStillNotExecuted::ExecTaskStillNotExecuted(std::string issuer)
		: _attempt(0), _attempts(0), Event(issuer) {
	_task_descriptor = 0;
}
ExecTaskStillNotExecuted::ExecTaskStillNotExecuted(
		TaskDescriptor* task_descriptor, unsigned int attempt,
		unsigned int attempts, std::string issuer) : _attempt(attempt),
		_attempts(attempts), Event(issuer) {
	_task_descriptor = task_descriptor;
}
ExecTaskStillNotExecuted::~ExecTaskStillNotExecuted(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
TaskExecuted::TaskExecuted(void) : Event(std::string("W")) {
	_task_descriptor = 0;
}
TaskExecuted::TaskExecuted(std::string issuer) : Event(issuer) {
	_task_descriptor = 0;
}
TaskExecuted::TaskExecuted(TaskDescriptor* task_descriptor,
		std::string issuer) : Event(issuer) {
	_task_descriptor = task_descriptor;
}
TaskExecuted::~TaskExecuted(void) { }
/*----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/-----/----*/
WorkerDescriptorSent::WorkerDescriptorSent(void) : Event(std::string("W")) {
	_worker_descriptor = 0;
	_addr = 0;
}
WorkerDescriptorSent::WorkerDescriptorSent(std::string issuer) : Event(issuer) {
	_worker_descriptor = 0;
	_addr = 0;
}
WorkerDescriptorSent::WorkerDescriptorSent(WorkerDescriptor* worker_descriptor,
		Address* addr, std::string issuer) : Event(issuer) {
	_worker_descriptor = worker_descriptor;
	_addr = addr;
}
WorkerDescriptorSent::~WorkerDescriptorSent(void) { }
//-----------------------------------------------------------------------------
// Public members and static members
/* Gets current timestamp */
std::string XmlSynLogger::LogTimestamp(XmlSynLoggerTimestampMode mode) {
	using namespace boost::posix_time;
	using namespace boost::local_time;
	using namespace boost::gregorian;
	std::string final_stamp;
	ptime t(microsec_clock::universal_time());
	if (mode == kUseLocalTime) t = ptime(microsec_clock::local_time());
	final_stamp = to_simple_string(t);
	return final_stamp;
}
/* Operators */
XmlSynLogger& XmlSynLogger::operator<<(const Event& e) {
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		xercesc::DOMElement* body_el =
				xmldoc->createElement(xercesc::XMLString::transcode("Body"));
		body_el->setTextContent(xercesc::XMLString::transcode(
				e.GetBody().c_str()));
		event_el->appendChild(body_el);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (Event) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const Error& e) {
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kErrorLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		xercesc::DOMElement* body_el =
				xmldoc->createElement(xercesc::XMLString::transcode("Description"));
		body_el->setTextContent(xercesc::XMLString::transcode(
				e.GetDescription().c_str()));
		event_el->appendChild(body_el);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (Error) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const WorkflowSubmitted& e) {
	xml::XmlIncapsulator xi_wf;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wf_node = 0;
		// Getting node for workflow
		if (e.GetWorkflow() != 0) {
			xi_wf << *(e.GetWorkflow());
			std::string wf_xml = xi_wf.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wf_xml.c_str(),
					wf_xml.size(), "dummy");
			xercesc::XercesDOMParser* wf_parser = new xercesc::XercesDOMParser();
			wf_parser->parse(myxml_buf);
			wf_node = this->GetXmlDocument()->importNode(
					wf_parser->getDocument()->getDocumentElement(), true);
			delete wf_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kWorkflowSubmittedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wf_node != 0) event_el->appendChild(wf_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (WorkflowSubmitted) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const WorkflowEnqueued& e) {
	xml::XmlIncapsulator xi_wf;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wf_node = 0;
		// Getting node for workflow
		if (e.GetWorkflow() != 0) {
			xi_wf << *(e.GetWorkflow());
			std::string wf_xml = xi_wf.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wf_xml.c_str(),
					wf_xml.size(), "dummy");
			xercesc::XercesDOMParser* wf_parser = new xercesc::XercesDOMParser();
			wf_parser->parse(myxml_buf);
			wf_node = this->GetXmlDocument()->importNode(
					wf_parser->getDocument()->getDocumentElement(), true);
			delete wf_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kWorkflowEnqueuedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wf_node != 0) event_el->appendChild(wf_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (WorkflowEnqueued) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const WorkflowDequeued& e) {
	xml::XmlIncapsulator xi_wf;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wf_node = 0;
		// Getting node for workflow
		if (e.GetWorkflow() != 0) {
			xi_wf << *(e.GetWorkflow());
			std::string wf_xml = xi_wf.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wf_xml.c_str(),
					wf_xml.size(), "dummy");
			xercesc::XercesDOMParser* wf_parser = new xercesc::XercesDOMParser();
			wf_parser->parse(myxml_buf);
			wf_node = this->GetXmlDocument()->importNode(
					wf_parser->getDocument()->getDocumentElement(), true);
			delete wf_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kWorkflowDequeuedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wf_node != 0) event_el->appendChild(wf_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (WorkflowDequeued) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const TaskSent& e) {
	xml::XmlIncapsulator xi_ad, xi_td;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* ad_node = 0;
		xercesc::DOMNode* td_node = 0;
		// Getting node for address
		if (e.GetAddress() != 0) {
			xi_ad << *(e.GetAddress());
			std::string ad_xml = xi_ad.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)ad_xml.c_str(),
					ad_xml.size(), "dummy");
			xercesc::XercesDOMParser* ad_parser = new xercesc::XercesDOMParser();
			ad_parser->parse(myxml_buf);
			ad_node = this->GetXmlDocument()->importNode(
					ad_parser->getDocument()->getDocumentElement(), true);
			delete ad_parser;
		}
		// Getting node for task descriptor
		if (e.GetTaskDescriptor() != 0) {
			xi_td << *(e.GetTaskDescriptor());
			std::string td_xml = xi_td.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)td_xml.c_str(),
					td_xml.size(), "dummy");
			xercesc::XercesDOMParser* td_parser = new xercesc::XercesDOMParser();
			td_parser->parse(myxml_buf);
			td_node = this->GetXmlDocument()->importNode(
					td_parser->getDocument()->getDocumentElement(), true);
			delete td_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kTaskSentEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (ad_node != 0) event_el->appendChild(ad_node);
		if (td_node != 0) event_el->appendChild(td_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (TaskSent) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const TaskReceived& e) {
	xml::XmlIncapsulator xi_ad, xi_td;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* ad_node = 0;
		xercesc::DOMNode* td_node = 0;
		// Getting node for address
		if (e.GetAddress() != 0) {
			xi_ad << *(e.GetAddress());
			std::string ad_xml = xi_ad.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)ad_xml.c_str(),
					ad_xml.size(), "dummy");
			xercesc::XercesDOMParser* ad_parser = new xercesc::XercesDOMParser();
			ad_parser->parse(myxml_buf);
			ad_node = this->GetXmlDocument()->importNode(
					ad_parser->getDocument()->getDocumentElement(), true);
			delete ad_parser;
		}
		// Getting node for task descriptor
		if (e.GetTaskDescriptor() != 0) {
			xi_td << *(e.GetTaskDescriptor());
			std::string td_xml = xi_td.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)td_xml.c_str(),
					td_xml.size(), "dummy");
			xercesc::XercesDOMParser* td_parser = new xercesc::XercesDOMParser();
			td_parser->parse(myxml_buf);
			td_node = this->GetXmlDocument()->importNode(
					td_parser->getDocument()->getDocumentElement(), true);
			delete td_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kTaskReceivedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (ad_node != 0) event_el->appendChild(ad_node);
		if (td_node != 0) event_el->appendChild(td_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (TaskReceived) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const WorkerDescriptorAdded& e) {
	xml::XmlIncapsulator xi_wd;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wd_node = 0;
		// Getting node for worker descripto
		if (e.GetWorkerDescriptor() != 0) {
			xi_wd << *(e.GetWorkerDescriptor());
			std::string wd_xml = xi_wd.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wd_xml.c_str(),
					wd_xml.size(), "dummy");
			xercesc::XercesDOMParser* wd_parser = new xercesc::XercesDOMParser();
			wd_parser->parse(myxml_buf);
			wd_node = this->GetXmlDocument()->importNode(
					wd_parser->getDocument()->getDocumentElement(), true);
			delete wd_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kWorkerDescriptorAddedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wd_node != 0) event_el->appendChild(wd_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (WorkerDescriptorAdded) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const WorkerDescriptorUpdated& e) {
	xml::XmlIncapsulator xi_wd;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wd_node = 0;
		// Getting node for worker descriptor
		if (e.GetWorkerDescriptor() != 0) {
			xi_wd << *(e.GetWorkerDescriptor());
			std::string wd_xml = xi_wd.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wd_xml.c_str(),
					wd_xml.size(), "dummy");
			xercesc::XercesDOMParser* wd_parser = new xercesc::XercesDOMParser();
			wd_parser->parse(myxml_buf);
			wd_node = this->GetXmlDocument()->importNode(
					wd_parser->getDocument()->getDocumentElement(), true);
			delete wd_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kWorkerDescriptorUpdatedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wd_node != 0) event_el->appendChild(wd_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (WorkerDescriptorUpdated) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const TaskEnqueued& e) {
	xml::XmlIncapsulator xi_td;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* td_node = 0;
		// Getting node for task descriptor
		if (e.GetTaskDescriptor() != 0) {
			xi_td << *(e.GetTaskDescriptor());
			std::string td_xml = xi_td.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)td_xml.c_str(),
					td_xml.size(), "dummy");
			xercesc::XercesDOMParser* td_parser = new xercesc::XercesDOMParser();
			td_parser->parse(myxml_buf);
			td_node = this->GetXmlDocument()->importNode(
					td_parser->getDocument()->getDocumentElement(), true);
			delete td_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kTaskEnqueuedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (td_node != 0) event_el->appendChild(td_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (TaskEnqueued) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const TaskDequeued& e) {
	xml::XmlIncapsulator xi_td;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* td_node = 0;
		// Getting node for task descriptor
		if (e.GetTaskDescriptor() != 0) {
			xi_td << *(e.GetTaskDescriptor());
			std::string td_xml = xi_td.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)td_xml.c_str(),
					td_xml.size(), "dummy");
			xercesc::XercesDOMParser* td_parser = new xercesc::XercesDOMParser();
			td_parser->parse(myxml_buf);
			td_node = this->GetXmlDocument()->importNode(
					td_parser->getDocument()->getDocumentElement(), true);
			delete td_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kTaskDequeuedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (td_node != 0) event_el->appendChild(td_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (TaskDequeued) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const StatusUpdateLoop& e) {
	xml::XmlIncapsulator xi_wdo, xi_wd;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wdo_node = 0;
		xercesc::DOMNode* wd_node = 0;
		xercesc::DOMNode* sc_node = 0;
		// Getting node for old worker descriptor
		if (e.GetOldWorkerDescriptor() != 0) {
			xi_wdo << *(e.GetOldWorkerDescriptor());
			std::string wdo_xml = xi_wdo.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wdo_xml.c_str(),
					wdo_xml.size(), "dummy");
			xercesc::XercesDOMParser* wdo_parser = new xercesc::XercesDOMParser();
			wdo_parser->parse(myxml_buf);
			wdo_node = this->GetXmlDocument()->importNode(
					wdo_parser->getDocument()->getDocumentElement(), true);
			// Adding flag parameter
			xercesc::DOMElement* wdo_el =
					dynamic_cast<xercesc::DOMElement*>(wdo_node);
			wdo_el->setAttribute(xercesc::XMLString::transcode("flag"),
					xercesc::XMLString::transcode("old"));
			delete wdo_parser;
		}
		// Getting node for updated worker descriptor
		if (e.GetWorkerDescriptor() != 0) {
			xi_wd << *(e.GetWorkerDescriptor());
			std::string wd_xml = xi_wd.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wd_xml.c_str(),
					wd_xml.size(), "dummy");
			xercesc::XercesDOMParser* wd_parser = new xercesc::XercesDOMParser();
			wd_parser->parse(myxml_buf);
			wd_node = this->GetXmlDocument()->importNode(
					wd_parser->getDocument()->getDocumentElement(), true);
			// Adding flag parameter
			xercesc::DOMElement* wd_el =
					dynamic_cast<xercesc::DOMElement*>(wd_node);
			wd_el->setAttribute(xercesc::XMLString::transcode("flag"),
					xercesc::XMLString::transcode("new"));
			delete wd_parser;
		}
		// Getting node for changed status
		if (e.GetOldWorkerDescriptor() != 0  && e.GetWorkerDescriptor() != 0) {
			std::string sc_xml = std::string("<IsStatusChanged>");
			bool changed = (*(e.GetOldWorkerDescriptor()) ==
					*(e.GetWorkerDescriptor()));
			if (changed) sc_xml += std::string("true");
			else sc_xml += std::string("false");
			sc_xml += std::string("</IsStatusChanged>");
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)sc_xml.c_str(),
					sc_xml.size(), "dummy");
			xercesc::XercesDOMParser* sc_parser = new xercesc::XercesDOMParser();
			sc_parser->parse(myxml_buf);
			sc_node = this->GetXmlDocument()->importNode(
					sc_parser->getDocument()->getDocumentElement(), true);
			delete sc_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kStatusUpdateLoopEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wdo_node != 0) event_el->appendChild(wdo_node);
		if (wd_node != 0) event_el->appendChild(wd_node);
		if (sc_node != 0) event_el->appendChild(sc_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (StatusUpdateLoop) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const TDCCollectorLoopExec& e) {
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* cl_node = 0;
		// Getting node for collected elements
		{
			std::string cl_xml = std::string("<CollectedEntries>");
			cl_xml += boost::lexical_cast<std::string>(e.GetCollected());
			cl_xml += std::string("</CollectedEntries>");
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)cl_xml.c_str(),
					cl_xml.size(), "dummy");
			xercesc::XercesDOMParser* cl_parser = new xercesc::XercesDOMParser();
			cl_parser->parse(myxml_buf);
			cl_node = this->GetXmlDocument()->importNode(
					cl_parser->getDocument()->getDocumentElement(), true);
			delete cl_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kTDCCollectorLoopExecEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		event_el->appendChild(cl_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (TDCCollectorLoopExec) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const ExecTaskStillNotExecuted& e) {
	xml::XmlIncapsulator xi_td;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* td_node = 0;
		xercesc::DOMNode* at_node = 0;
		xercesc::DOMNode* ats_node = 0;
		// Getting node for task descriptor
		if (e.GetTaskDescriptor() != 0) {
			xi_td << *(e.GetTaskDescriptor());
			std::string td_xml = xi_td.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)td_xml.c_str(),
					td_xml.size(), "dummy");
			xercesc::XercesDOMParser* td_parser = new xercesc::XercesDOMParser();
			td_parser->parse(myxml_buf);
			td_node = this->GetXmlDocument()->importNode(
					td_parser->getDocument()->getDocumentElement(), true);
			delete td_parser;
		}
		// Getting node for attempt
		{
			std::string at_xml = std::string("<Attempt>");
			at_xml += boost::lexical_cast<std::string>(e.GetAttempt());
			at_xml += std::string("</Attempt>");
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)at_xml.c_str(),
					at_xml.size(), "dummy");
			xercesc::XercesDOMParser* at_parser = new xercesc::XercesDOMParser();
			at_parser->parse(myxml_buf);
			at_node = this->GetXmlDocument()->importNode(
					at_parser->getDocument()->getDocumentElement(), true);
			delete at_parser;
		}
		// Getting node for attempts
		{
			std::string ats_xml = std::string("<Attempts>");
			ats_xml += boost::lexical_cast<std::string>(e.GetAttempts());
			ats_xml += std::string("</Attempts>");
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)ats_xml.c_str(),
					ats_xml.size(), "dummy");
			xercesc::XercesDOMParser* ats_parser = new xercesc::XercesDOMParser();
			ats_parser->parse(myxml_buf);
			ats_node = this->GetXmlDocument()->importNode(
					ats_parser->getDocument()->getDocumentElement(), true);
			delete ats_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kExecTaskStillNotExecutedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (td_node != 0) event_el->appendChild(td_node);
		if (at_node != 0) event_el->appendChild(at_node);
		if (ats_node != 0) event_el->appendChild(ats_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " <<
				"Error in event (ExecTaskStillNotExecuted) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const TaskExecuted& e) {
	xml::XmlIncapsulator xi_td;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* td_node = 0;
		// Getting node for task descriptor
		if (e.GetTaskDescriptor() != 0) {
			xi_td << *(e.GetTaskDescriptor());
			std::string td_xml = xi_td.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)td_xml.c_str(),
					td_xml.size(), "dummy");
			xercesc::XercesDOMParser* td_parser = new xercesc::XercesDOMParser();
			td_parser->parse(myxml_buf);
			td_node = this->GetXmlDocument()->importNode(
					td_parser->getDocument()->getDocumentElement(), true);
			delete td_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kTaskExecutedEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (td_node != 0) event_el->appendChild(td_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " <<	"Error in event (TaskExecuted) attachment!" << std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
XmlSynLogger& XmlSynLogger::operator<<(const WorkerDescriptorSent& e) {
	xml::XmlIncapsulator xi_wd;
	boost::mutex::scoped_lock lck(this->_log_dom_monitor); /* Locks mutex */
	try {
		xercesc::DOMNode* wd_node = 0;
		// Getting node for worker descriptor
		if (e.GetWorkerDescriptor() != 0) {
			xi_wd << *(e.GetWorkerDescriptor());
			std::string wd_xml = xi_wd.GetXml();
			xercesc::MemBufInputSource myxml_buf((const XMLByte*)wd_xml.c_str(),
					wd_xml.size(), "dummy");
			xercesc::XercesDOMParser* wd_parser = new xercesc::XercesDOMParser();
			wd_parser->parse(myxml_buf);
			wd_node = this->GetXmlDocument()->importNode(
					wd_parser->getDocument()->getDocumentElement(), true);
			delete wd_parser;
		}
		// Building main node (event)
		xercesc::DOMDocument* xmldoc = this->GetXmlDocument();
		xercesc::DOMElement* root = this->GetXmlEventsElement();
		xercesc::DOMElement* event_el =
				xmldoc->createElement(xercesc::XMLString::transcode(
						kWorkerDescriptorSentEventLogTag.c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kTimestampLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetTimestamp().c_str()));
		event_el->setAttribute(xercesc::XMLString::transcode(
				kIssuerLogTagParameter.c_str()),
				xercesc::XMLString::transcode(e.GetIssuer().c_str()));
		if (wd_node != 0) event_el->appendChild(wd_node);
		root->appendChild(event_el);
	} catch(std::exception& ex) {
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "Error in event (WorkerDescriptorSent) attachment!" <<
				std::endl;
	}
	return *this; // Unlocks automatically on out of scope
}
//-----------------------------------------------------------------------------
// Private members
/* Initializing file */
void XmlSynLogger::InitParser(XmlSynLoggerOpenMode open_mode,
		XmlSynLoggerCompositionMode comp_mode) {
	/* When mode is set to kLogOpenOverwriteMode then the file must be
	 * recreated (cleared and filled in future, the previous content must not
	 * be preserved). */
	this->_parser->setValidationScheme(xercesc::XercesDOMParser::Val_Never);
	this->_parser->setDoSchema(false);
	this->_parser->setLoadExternalDTD(false);
	if (open_mode == kLogOpenKeepFile) {
		// Keep file: parse the file, the initialization string for parser
		this->_parser->parse(this->_log_filename.c_str());
	} else {
		// Default: parse a string, the initialization string for parser
		std::string initxml =
				std::string("<?xml-stylesheet ") +
				std::string("type=\"text/xsl\" ") +
				std::string("href=\"") + kLogDefXslStylesheet +
				std::string("\"?>") +
				std::string("<Log>") +
					std::string("<Info>") +
						std::string("<CreationDate>") +
						XmlSynLogger::LogTimestamp() +
						std::string("</CreationDate>") +
						std::string("<Peer>") +
						this->_peer_identifier +
						std::string("</Peer>") +
					std::string("</Info>") +
					std::string("<Events></Events>") +
				std::string("</Log>");
		xercesc::MemBufInputSource initxml_buf((const XMLByte*)initxml.c_str(),
				initxml.size(), "dummy");
		this->_parser->parse(initxml_buf);
	}
}
/* Log writer execution flow */
void XmlSynLogger::Exec_LogFileWriter() {
	// This creates a new thread for managing agreement
	FileCTAProxy cta_proxy(this->_log_cta_port, this->_log_filename,
			kOpenOverwrite);
	// Processing loop
	for (;;) {
		// 1) Processing tree
		try {
			xercesc::DOMImplementation *implementation =
					xercesc::DOMImplementationRegistry::getDOMImplementation(
							xercesc::XMLString::transcode("LS"));
			xercesc::DOMLSSerializer *serializer =
					((xercesc::DOMImplementationLS*)implementation)->createLSSerializer();
			if (serializer->getDomConfig()->canSetParameter(
					xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
				serializer->getDomConfig()->setParameter(
						xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
			serializer->setNewLine(xercesc::XMLString::transcode("\r\n"));
			xercesc::DOMLSOutput *output =
					((xercesc::DOMImplementationLS*)implementation)->createLSOutput();
			// Starting critical region
			boost::mutex::scoped_lock lck_dom(this->_log_dom_monitor); /* Locks mutex */
			// Write the serialized output to the destination.
			XMLCh* res = serializer->writeToString(this->GetXmlDocument());
			lck_dom.unlock(); /* Unlock */
			std::string xml_to_write = xercesc::XMLString::transcode(res);
			serializer->release();
#ifdef PRINT_LOG_TO_WRITE
			std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
					": " << "Tree parsed, xml to write: " << "##" <<
					std::endl << xml_to_write << "##" << std::endl;
#endif
			// 2) Processing file
			cta_proxy << xml_to_write;
			// Ending critical region
		} catch (std::exception& e) {
			std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
					": " << "ERROR! " << e.what() <<
					" Going to next cycle..." << std::endl;
		}
		std::cout << kXmlSynLoggerEtq << "-" << this->_log_filename <<
				": " << "File written! Now sleeping to reactivate later!" <<
				std::endl;
		// Waiting
		boost::this_thread::sleep(boost::posix_time::milliseconds(
				kLogWriterWritePeriodMsec));
	}
}
